library(R.matlab)
library(xts)
data=readMat('D:/annexnew/Problem1/vixlarge.mat')

VIX=c()
for(i in 1:6862)
{
  date=as.character(data$DATE[i])
  date=substr(date,7,16)
  VIX[date]=data$VIX[i]
}

VIX=as.xts(VIX)
plot(VIX)

allAIC=c()
allBIC=c()
for(i in 1:22)
{
  print(i)
  model=arima(VIX, order = c(i, 0, 0))
  allAIC[i]=model$aic
  allBIC[i]=BIC(model)
}

bestAIC=which(allAIC==min(allAIC),arr.ind=TRUE)
bestBIC=which(allBIC==min(allBIC),arr.ind=TRUE)
cat("AIC Best Model is AR",bestAIC)
cat(" AIC:",allAIC[bestAIC])
print()
cat("BIC Best Model is AR",bestBIC)
cat("AIC:",allBIC[bestBIC])

